"use strict";

function createVM(selector, data, func) {
    var orgData = {
        PageShow: false //表单验证常用字段
        ,
        rules: {
            notNull: {
                validate: function validate(val) {
                    return !!isTrue(val);
                },
                message: '不能为空'
            },
            min: function min(_min) {
                return {
                    validate: function validate(val) {
                        return val.length >= _min;
                    },
                    message: '字符长度不能小于' + _min
                };
            },
            max: function max(_max) {
                return {
                    validate: function validate(val) {
                        return val.length <= _max;
                    },
                    message: '字符长度不能大于' + _max
                };
            },
            len: function len(_len) {
                return {
                    validate: function validate(val) {
                        return val.length === _len;
                    },
                    message: '字符长度必须为' + _len
                };
            },
            isPhone: {
                validate: function validate(val) {
                    var myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/;

                    if (!myreg.test(val)) {
                        return false;
                    } else {
                        return true;
                    }
                },
                message: '手机号不合法'
            },
            isNum: {
                validate: function validate(val) {
                    var myreg = /^([0-9\.])+?$/;

                    if (!myreg.test(val)) {
                        return false;
                    } else {
                        return true;
                    }
                },
                message: '请输入数字'
            }
        },
        PageRefreshing: false,
        PageLoadingMore: false,
        selectChoices:{}
        // picFilePath: picFilePath
    };

    for (var i in data) {
        orgData[i] = data[i];
    }

    var methods = {};
    methods["goback"] = goback;
    methods["goto"] = goto;
    methods["autoSubmit"] = autoSubmit
    methods["isFalse"] = isFalse;
    methods["isTrue"] = isTrue;
    methods["boole"] = isTrue;

    methods["OnLoad"] = function () {
    };

    methods["OnShow"] = function () {
    };

    methods["OnHide"] = function () {
    };

    methods["OnPullDownRefresh"] = function () {
    };

    methods["OnPullUpLoadMore"] = function () {
    };
    methods["goback"] = function () {
        history.go(-1)
    };


    //检查角色权限的方法
    // methods["CanIUse"] = CanIUse; // methods["RoleWenYuan"]                 = RoleWenYuan;

    //常用的内置模板参数处理
    methods["isPic"] = isPic;
    methods["inArray"] = function (name,value) {
        if (inArray(value,Page.en[name]) > -1) {
            return true
        }else {
            return false
        }
    };
    methods["checkBox"] = function (name,value) {
        console.log(value)
        console.log(Page.en[name])
        if (inArray(value,Page.en[name]) > -1) {
            console.log("in array")
            Page.en[name] = arrayDelValue(Page.en[name],value)
        }else {
            Page.en[name].push(value)
            console.log("not in array")
        }
    };
    methods["picRoot"] = getAssetBaseUrl;
    methods["picUrl"] = picUrl;
    methods["picBg"] = function (pic) {
        return "background-size: cover;background-image: url('" + picUrl(pic) +"');"

    };


    for (var i in func) {
        methods[i] = func[i];
    }

    var vue = new Vue({
        el: selector,
        delimiters: ['[[', ']]'],
        data: orgData,
        //基础数据
        methods: methods
    });
    return vue;
}

function isPic(path) {
    if (!isTrue(path)) {
        return false;
    }

    var suffix = get_suffix(path).toUpperCase().substring(1);
    log(suffix);

    if (suffix != "BMP" && suffix != "JPG" && suffix != "JPEG" && suffix != "PNG" && suffix != "GIF") {
        return false;
    } else {
        return true;
    }
}

function get_suffix(filename) {
    var pos = filename.lastIndexOf('.');
    var suffix = '';

    if (pos != -1) {
        suffix = filename.substring(pos);
    }

    return suffix;
}

function autoSubmit() {
    var _this = this
    console.log($("textarea.UEditor").val());
    console.log(this);

    // 检查是否有文章类型的字段
    $("textarea.UEditor").each(function () {
        var name = $(this).attr("name")
        var val = $(this).val();
        eval("Page.en." + name + "=val")
    });


    this.$refs.form.validate().then(function (result) {

        if (result) {
            _this.submit()
        } else {
            showToast("请检查表单输入是否完整")
        }
    });
}